package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.ChinatopcreditService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwThirdback;
import com.meirengu.webview.service.KwThirdbackService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 畅愿
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class ChinatopcreditServiceImpl implements ChinatopcreditService {

    Logger logger = LoggerFactory.getLogger(ChinatopcreditServiceImpl.class);
    @Autowired
    KwThirdbackService kwThirdbackService;

    /**
     * @param
     * @return
     */

    public Integer applyLoan(String userId, String name, String mobile, String city, String vocation, String incomeVal) throws Exception {

        String url = ConfigUtil.getConfig("chinatopcredit.url");
        String channel = ConfigUtil.getConfig("chinatopcredit.channel");
        url = url + channel;


        Map<String, String> params = new HashMap<>();
        params.put("tokenId", userId);
        params.put("name", name);
        params.put("telephone", mobile);
        params.put("city", city);
        params.put("vocation", vocation);
        params.put("income", incomeVal);


        //接口调用信息表
        KwThirdback thirdback = new KwThirdback();
        thirdback.setInterfaceCode("ctc");
        thirdback.setInterfaceName("中腾信官方");
        thirdback.setMobile(mobile);
        thirdback.setSendUrl(url.toString());
        thirdback.setParams(params.toString());
        thirdback.setCode(Constants.BUSINESS_STATUS_FAIL);

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(url, params);
            int code = hr.getStatusCode();
            if (code == 200) {
                thirdback.setBackUrl(hr.toString());
                thirdback.setHttpCode(200);
                thirdback.setHttpContent(hr.toString());

                logger.info("==ChinatopcreditServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                content = content.replace("?", "").replace("(", "").replace(")", "");
                logger.info("content:{}", content);
                JSONObject returnMsg = JSONObject.parseObject(content);
                String result = returnMsg.getString("result");
                if ("success".equals(result)) {
                    thirdback.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    thirdback.setMessage("成功");
                    logger.info("==ChinatopcreditServiceImpl.applyLoan business success");
                } else {
                    thirdback.setCode(Constants.BUSINESS_STATUS_OTHER);
                    thirdback.setMessage("result: " + result);
                    logger.warn("==ChinatopcreditServiceImpl.applyLoan business fail");
                }
            } else {
                thirdback.setBackUrl(hr.toString());
                thirdback.setHttpCode(code);
                thirdback.setHttpContent(hr.toString());
                logger.warn("==ChinatopcreditServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            thirdback.setHttpCode(408);
            thirdback.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwThirdbackService.insert(thirdback);
            return thirdback.getCode();
        }


    }

}
